/***********************************************************
 
 File name: UIViewController+Base.m
 Author:    xuwei
 Description:
 对UIViewController的封装：目前主要写了对导航栏的封装
 
 2018/02/27: Created
 
 ************************************************************/

#import "UIViewController+Base.h"

@implementation UIViewController (Base)

/**
 有返回按钮的导航栏

 @param title 导航栏的title
 */
- (void)setNavigationWithTitle:(NSString *)title {
    [self setNavigationBarWithBGColor:kNavigationColor
                                title:title
                           titleColor:[UIColor whiteColor]
                            imageName:@"back"
                              seletor:@selector(popViewController)];
}

/**
 自定义返回按钮的导航栏

 @param title 导航栏的title
 @param action 返回方法
 */
- (void)setNavigationWithTitle:(NSString *)title
                       seletor:(SEL)action {
    
    [self setNavigationBarWithBGColor:kNavigationColor
                                title:title
                           titleColor:[UIColor whiteColor]
                            imageName:@"back"
                              seletor:action];
}

/**
 设置导航栏的背景色、title、titleColor等

 @param hexColor 背景色
 @param title title
 @param color titleColor
 @param imageName imageName
 @param action seletor
 */
- (void)setNavigationBarWithBGColor:(NSString *)hexColor
                              title:(NSString *)title
                         titleColor:(UIColor *)color
                          imageName:(NSString *)imageName
                            seletor:(SEL)action {
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:hexColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = title;
    NSDictionary *dict = [NSDictionary dictionaryWithObject:color forKey: NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    
    if (action == nil) {
        self.navigationItem.leftBarButtonItem = nil;
    } else {
        UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:action];
        self.navigationItem.leftBarButtonItem = leftItem;
    }
    
    // 显示默认阴影效果
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

/**
 带返回按钮的导航栏

 @param title title
 @param action 返回事件
 */
- (void)setNavigationWithTitle:(NSString *)title
               withBackseletor:(SEL)action {
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:kNavigationColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = title;
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey: NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    
    UIImage *image = [[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:action];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 40, 40);
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setTitle:@"关闭" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    
    UIBarButtonItem *titleItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItems = @[leftItem,titleItem];
    
    //    //使导航栏左按钮左边距为0
    //    UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //    nagetiveSpacer.width = -2;
    //    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:nagetiveSpacer,leftItem,titleItem,nil];
    
    // 显示默认阴影效果
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

/**
 pop回上一个
 */
- (void)popViewController {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
